package com.mxpio.erp.equipment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mxpio.erp.common.equipment.entity.EquipmentCheckDetail;
import com.mxpio.erp.common.equipment.entity.EquipmentCheckDetailParam;
import com.mxpio.erp.common.equipment.entity.EquipmentCommParametersDetail;
import com.mxpio.erp.common.equipment.service.EquipmentCheckDetailParamService;
import com.mxpio.erp.common.equipment.service.EquipmentCheckDetailService;
import com.mxpio.erp.common.equipment.service.EquipmentCommParametersDetailService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 点巡检标准明细
 * @Author: jeecg-boot
 * @Date:   2021-09-15
 * @Version: V1.0
 */
@Service
public class EquipmentCheckDetailServiceImpl extends BaseServiceImpl<EquipmentCheckDetail> implements EquipmentCheckDetailService {


    @Autowired
    private EquipmentCheckDetailParamService equipmentCheckDetailParamService;

    @Autowired
    private EquipmentCommParametersDetailService equipmentCommParametersDetailService;

	@Override
	public List<EquipmentCheckDetail> selectByMainId(String mainId) {
        Criteria c = Criteria.create().addCriterion("categoryId", Operator.EQ, mainId);
	    return list(EquipmentCheckDetail.class,c);
	}


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveFromParams(EquipmentCheckDetail equipmentCheckDetail) {
        save(equipmentCheckDetail);

        if (equipmentCheckDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> EquipmentCommParametersDetails = equipmentCommParametersDetailService.selectByMainId(equipmentCheckDetail.getParamId());
            if (!CollectionUtils.isEmpty(EquipmentCommParametersDetails)) {
                EquipmentCommParametersDetails.forEach(params -> {
                    EquipmentCheckDetailParam checkDetailParam = new EquipmentCheckDetailParam();
                    BeanUtil.copyProperties(params, checkDetailParam, "id", "createBy", "createTime", "updateBy", "updateTime", "sysOrgCode");
                    checkDetailParam.setCommParamId(params.getId());
                    checkDetailParam.setDetailId(equipmentCheckDetail.getId());
                    equipmentCheckDetailParamService.save(checkDetailParam);
                });
            }
        }

    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editFromParams(EquipmentCheckDetail equipmentCheckDetail) {

	    // 更新明细主内容
        update(equipmentCheckDetail);

        // 删除明细选项内容
        equipmentCheckDetailParamService.deleteByMainId(equipmentCheckDetail.getId());

        if (equipmentCheckDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> equipmentCommParametersDetails = equipmentCommParametersDetailService.selectByMainId(equipmentCheckDetail.getParamId());
            if (!CollectionUtils.isEmpty(equipmentCommParametersDetails)) {
                equipmentCommParametersDetails.forEach(params -> {
                    EquipmentCheckDetailParam checkDetailParam = new EquipmentCheckDetailParam();
                    BeanUtil.copyProperties(params, checkDetailParam, "id", "createBy", "createTime", "updateBy", "updateTime", "createDept");
                    checkDetailParam.setCommParamId(params.getId());
                    checkDetailParam.setDetailId(equipmentCheckDetail.getId());
                    equipmentCheckDetailParamService.save(checkDetailParam);
                });
            }
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDetailAndParams(String detailId) {
        delete(EquipmentCheckDetail.class,detailId);

        // 删除明细选项内容
        equipmentCheckDetailParamService.deleteByMainId(detailId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatchDetailAndParams(List<String> detailIds) {

        JpaUtil.lind(EquipmentCheckDetailParam.class).in("detailId", detailIds).delete();

        Criteria c = Criteria.create().addCriterion("id", Operator.IN, detailIds);
        deleteBatch(EquipmentCheckDetail.class,c);
    }
}
